package servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BasketServlet extends HttpServlet {


	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProccessing(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doProccessing(req, resp);
	}

	private void doProccessing(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		String [] item = req.getParameterValues("item");
		ArrayList<String> amounts = new ArrayList<>();
		if(item.length > 0)
		{
			for(String s : item)
			{
				amounts.add(req.getParameter("amount" +s));
			}
		}
		int i = 0;
		for(String s : item)
		{
			System.out.println("Item : " + s);
			System.out.println("Amount : " + amounts.get(i));
			i++;
		}
		req.getSession().setAttribute("item", item);
		req.getSession().setAttribute("amounts", amounts); 
		
		RequestDispatcher dispatch = req.getRequestDispatcher("Search_Results.jsp");
		dispatch.forward(req, resp);
	}

}
